/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.jr.swb.rest.mq.global;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ac.iie.di.ban.jr.rest.config.ConfigureUtil;
import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import static cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType.Struct;

/**
 *
 * @author QJP
 */
public class GlobalParas {

    public static Logger LOG = Logger.getLogger(GlobalParas.class);

    //properties name
    public final static String LOG4_CONFIGURATION = "log4j.properties";
    public final static String DEFAULT_CONFIGURATION = "configure.properties";

    //configuration.properties parameters
    public static String REST_SERVER_IP = "";
    public static Integer REST_SERVER_PORT = 0;
    public static Integer REST_SERVER_PARALLEL_SIZE = 0;

    public static String FILE_DIR = "";
    public static Integer FILE_SIZE = 5;
    public static Integer FILE_TIMEOUT = 5;
    //存放数据类型和与之对应的uri数组
    public static String DATATYPE_URI = null;
    public static HashMap<String, String> DATATYPE_URI_MAP = null;

    //针对不同推送数据类型进行字段列表集合初始化
    //1.热门微博(t_hot_microblog)接口
    public static List<Object> THOT_DATA_COLUMN_TYPE = null;
    public static List<String> THOT_COLUMNS_NAME_LIST;
    public static List<REFieldType> THOT_COLUMNS_TYPE_LIST;
    //2.大V热门微博
    public static List<Object> TVHOT_DATA_COLUMN_TYPE = null;
    public static List<String> TVHOT_COLUMNS_NAME_LIST;
    public static List<REFieldType> TVHOT_COLUMNS_TYPE_LIST;
    //3.热门话题
    public static List<Object> TMHOT_DATA_COLUMN_TYPE = null;
    public static List<String> TMHOT_COLUMNS_NAME_LIST;
    public static List<REFieldType> TMHOT_COLUMNS_TYPE_LIST;
    //4.热门话题（分类t_class_topic
    private static List<Object> TCHOT_DATA_COLUMN_TYPE = null;
    public static List<String> TCHOT_COLUMNS_NAME_LIST;
    public static List<REFieldType> TCHOT_COLUMNS_TYPE_LIST;
    //5.热搜词(t_hot_search)(
    private static List<Object> TSHOT_DATA_COLUMN_TYPE = null;
    public static List<String> TSHOT_COLUMNS_NAME_LIST;
    public static List<REFieldType> TSHOT_COLUMNS_TYPE_LIST;
    //6.日原创微博量接口orig_microblog
    private static List<Object> TORIG_DATA_COLUMN_TYPE = null;
    public static List<String> TORIG_COLUMNS_NAME_LIST;
    public static List<REFieldType> TORIG_COLUMNS_TYPE_LIST;
    //7.大V日原创微博量接口vorig_microblog
    private static List<Object> TVORIG_DATA_COLUMN_TYPE = null;
    public static List<String> TVORIG_COLUMNS_NAME_LIST;
    public static List<REFieldType> TVORIG_COLUMNS_TYPE_LIST;
    //8.在线用户数(t_online_user)
    private static List<Object> TOLUS_DATA_COLUMN_TYPE = null;
    public static List<String> TOLUS_COLUMNS_NAME_LIST;
    public static List<REFieldType> TOLUS_COLUMNS_TYPE_LIST;

    public static List<Object> DATA_COLUMN_TYPE = null;
    public static HashMap<String, String> COLUMN_TYPE_MAP = null;

    public static String CONSUMER_NAMESERVER = "";
    public static List<Object> CONSUMER_DATATYPE_TOPIC = null;
    public static HashMap<String, String> CONSUMER_DATATYPE_TOPIC_MAP = null;

    public static List<String> COLUMNS_NAME_LIST;
    public static List<REFieldType> COLUMNS_TYPE_LIST;

    private static volatile GlobalParas instanceDefConf;

    public GlobalParas(String confPropertiesName) throws ConfigurationException {
        String confPropertiesPath = ClassLoader.getSystemClassLoader().getResource(confPropertiesName).getFile();
        ConfigureUtil configureUtil = new ConfigureUtil();
        FileBasedConfiguration configuration = configureUtil.initConfiguration(confPropertiesPath);
        if (confPropertiesName.equals(DEFAULT_CONFIGURATION)) {
            initConf(configuration);
        }
    }

    public static GlobalParas getIstance(String confPropertiesName) throws ConfigurationException {
        if (confPropertiesName.equals(DEFAULT_CONFIGURATION)) {
            if (instanceDefConf == null) {
                synchronized (GlobalParas.class) {
                    if (instanceDefConf == null) {
                        instanceDefConf = new GlobalParas(confPropertiesName);
                    }
                }
            }
            return instanceDefConf;
        } else {
            LOG.error("Could Not Find Configuration: " + confPropertiesName);
            return null;
        }

    }

    public static void initConf(FileBasedConfiguration configuration) {

        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource(LOG4_CONFIGURATION));

        FILE_DIR = configuration.getString("file.dir");
        if (!FILE_DIR.endsWith("/")) {
            FILE_DIR += "/";
        }
        FILE_SIZE = configuration.getInt("file.size");
        FILE_TIMEOUT = configuration.getInt("file.timeout");
        //初始化数据类型和与之对应的uri信息
        DATATYPE_URI = configuration.getString("datatype.uri");
        DATATYPE_URI_MAP = new HashMap<>();
        DATATYPE_URI_MAP = initDatatypeUri();

        THOT_DATA_COLUMN_TYPE = new ArrayList<>();
        THOT_DATA_COLUMN_TYPE = configuration.getList("thot.data.column.type");

        TVHOT_DATA_COLUMN_TYPE = new ArrayList<>();
        TVHOT_DATA_COLUMN_TYPE = configuration.getList("tvhot.data.column.type");

        TMHOT_DATA_COLUMN_TYPE = new ArrayList<>();
        TMHOT_DATA_COLUMN_TYPE = configuration.getList("tmhot.data.column.type");

        TSHOT_DATA_COLUMN_TYPE = new ArrayList<>();
        TSHOT_DATA_COLUMN_TYPE = configuration.getList("tshot.data.column.type");

        TCHOT_DATA_COLUMN_TYPE = new ArrayList<>();
        TCHOT_DATA_COLUMN_TYPE = configuration.getList("tchot.data.column.type");

        TORIG_DATA_COLUMN_TYPE = new ArrayList<>();
        TORIG_DATA_COLUMN_TYPE = configuration.getList("torig.data.column.type");

        TVORIG_DATA_COLUMN_TYPE = new ArrayList<>();
        TVORIG_DATA_COLUMN_TYPE = configuration.getList("tvorig.data.column.type");

        TOLUS_DATA_COLUMN_TYPE = new ArrayList<>();
        TOLUS_DATA_COLUMN_TYPE = configuration.getList("tolus.data.column.type");

        CONSUMER_NAMESERVER = configuration.getString("consumer.nameserver");
        CONSUMER_DATATYPE_TOPIC = configuration.getList("consumer.topic");
        GlobalParas.COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.THOT_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.THOT_COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.TVHOT_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.TVHOT_COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.TMHOT_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.TMHOT_COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.TCHOT_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.TCHOT_COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.TSHOT_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.TSHOT_COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.TORIG_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.TORIG_COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.TVORIG_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.TVORIG_COLUMNS_TYPE_LIST = new ArrayList<>();
        GlobalParas.TOLUS_COLUMNS_NAME_LIST = new ArrayList<>();
        GlobalParas.TOLUS_COLUMNS_TYPE_LIST = new ArrayList<>();
        try {
            initFieldMapByType(THOT_DATA_COLUMN_TYPE, THOT_COLUMNS_NAME_LIST, THOT_COLUMNS_TYPE_LIST);
            initFieldMapByType(TVHOT_DATA_COLUMN_TYPE, TVHOT_COLUMNS_NAME_LIST, TVHOT_COLUMNS_TYPE_LIST);
            initFieldMapByType(TMHOT_DATA_COLUMN_TYPE, TMHOT_COLUMNS_NAME_LIST, TMHOT_COLUMNS_TYPE_LIST);
            initFieldMapByType(TCHOT_DATA_COLUMN_TYPE, TCHOT_COLUMNS_NAME_LIST, TCHOT_COLUMNS_TYPE_LIST);
            initFieldMapByType(TSHOT_DATA_COLUMN_TYPE, TSHOT_COLUMNS_NAME_LIST, TSHOT_COLUMNS_TYPE_LIST);
            initFieldMapByType(TORIG_DATA_COLUMN_TYPE, TORIG_COLUMNS_NAME_LIST, TORIG_COLUMNS_TYPE_LIST);
            initFieldMapByType(TVORIG_DATA_COLUMN_TYPE, TVORIG_COLUMNS_NAME_LIST, TVORIG_COLUMNS_TYPE_LIST);
            initFieldMapByType(TOLUS_DATA_COLUMN_TYPE, TOLUS_COLUMNS_NAME_LIST, TOLUS_COLUMNS_TYPE_LIST);
            CONSUMER_DATATYPE_TOPIC_MAP = new HashMap<>();
            CONSUMER_DATATYPE_TOPIC_MAP = initDatatypeTopic();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

//        LOG.info("iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii" + TVHOT_COLUMNS_TYPE_LIST);
    }

    private static void initFieldMapByType(List<Object> data_columns_type, List<String> columns_name_list, List<REFieldType> columns_type_list) {
        for (Object field : data_columns_type) {
            LOG.debug(field);
            switch (((String) field).split(":")[1]) {
                case "int": {
                    columns_name_list.add(((String) field).split(":")[0]);
                    columns_type_list.add(REFieldType.Int);
                    break;
                }
                case "timestamp":
                case "long": {
                    columns_name_list.add(((String) field).split(":")[0]);
                    columns_type_list.add(REFieldType.Long);
                    break;
                }
                case "string": {
                    columns_name_list.add(((String) field).split(":")[0]);
                    columns_type_list.add(REFieldType.String);
                    break;
                }
                case "strings": {
                    columns_name_list.add(((String) field).split(":")[0]);
                    columns_type_list.add(REFieldType.Strings);
                    break;
                }
                case "struct": {
                    columns_name_list.add(((String) field).split(":")[0]);
                    columns_type_list.add(REFieldType.Struct);
                    break;
                }
                case "structs": {
                    columns_name_list.add(((String) field).split(":")[0]);
                    columns_type_list.add(REFieldType.Structs);
                    break;
                }
                case "double": {
                    columns_name_list.add(((String) field).split(":")[0]);
                    columns_type_list.add(REFieldType.Double);
                    break;
                }
                default: {
                    LOG.error("not find field type: " + ((String) field).split(":")[1]);
                    break;
                }
            }
        }
    }

    private static HashMap<String, String> initDatatypeUri() {
        HashMap<String, String> hashMap = new HashMap<>();
        String[] datatype_uri = DATATYPE_URI.split(";");
        for (String datatype_uri1 : datatype_uri) {
            LOG.info("data.uri map key = " + String.valueOf(datatype_uri1).split("@")[0] + "; value = " + String.valueOf(datatype_uri1).split("@")[1]);
            hashMap.put(String.valueOf(datatype_uri1).split("@")[0], String.valueOf(datatype_uri1).split("@")[1]);
        }
        return hashMap;
    }

    private static HashMap<String, String> initColumnTypeMap() {
        HashMap<String, String> hashMap = new HashMap<>();
        for (Object topicGroup : DATA_COLUMN_TYPE) {
            hashMap.put(String.valueOf(topicGroup).split(":")[0], String.valueOf(topicGroup).split(":")[1]);
        }
        return hashMap;
    }

    private static void initFieldMap() {
        for (Object field : DATA_COLUMN_TYPE) {
            LOG.debug(field);
            switch (((String) field).split(":")[1]) {
                case "int": {
                    COLUMNS_NAME_LIST.add(((String) field).split(":")[0]);
                    COLUMNS_TYPE_LIST.add(REFieldType.Int);
                    break;
                }
                case "timestamp":
                case "long": {
                    COLUMNS_NAME_LIST.add(((String) field).split(":")[0]);
                    COLUMNS_TYPE_LIST.add(REFieldType.Long);
                    break;
                }
                case "string": {
                    COLUMNS_NAME_LIST.add(((String) field).split(":")[0]);
                    COLUMNS_TYPE_LIST.add(REFieldType.String);
                    break;
                }
                case "strings": {
                    COLUMNS_NAME_LIST.add(((String) field).split(":")[0]);
                    COLUMNS_TYPE_LIST.add(REFieldType.Strings);
                    break;
                }
                case "struct": {
                    COLUMNS_NAME_LIST.add(((String) field).split(":")[0]);
                    COLUMNS_TYPE_LIST.add(REFieldType.Struct);
                    break;
                }
                case "structs": {
                    COLUMNS_NAME_LIST.add(((String) field).split(":")[0]);
                    COLUMNS_TYPE_LIST.add(REFieldType.Structs);
                    break;
                }
                case "double": {
                    COLUMNS_NAME_LIST.add(((String) field).split(":")[0]);
                    COLUMNS_TYPE_LIST.add(REFieldType.Double);
                    break;
                }
                default: {
                    LOG.error("not find field type: " + ((String) field).split(":")[1]);
                    break;
                }
            }
        }
    }

    private static HashMap<String, String> initDatatypeTopic() {
        HashMap<String, String> hashMap = new HashMap<>();
        for (Object object : CONSUMER_DATATYPE_TOPIC) {
            hashMap.put(String.valueOf(object).split(":")[0], String.valueOf(object).split(":")[1]);
        }
        return hashMap;
    }
}
